<?php
namespace App\Models\Account;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;


class Loginst extends  Model{

	protected $table='user_logout';
    protected $_db_select = 'remote_bgsi';

    public $_max_count = 0;
    public function __construct()
    {
        \App\Models\Area::RemoteBgsiSelectByArea(Request::get('area'));
        \Config::set("database.connections.remote_bgsi.database", "bgsi_".Request::get('area'));
    }
	public function paginate()
    {
    	$perPage = Request::get('per_page', 20);

        $page = Request::get('page', 1);

        $page = ($page-1)*$perPage;

        list($start, $end) = self::getInput();

        //$data = \DB::connection($this->selectdb())->table($tableAccount)->skip($page* $perPage)->take($perPage)->get();
        //$data = $data->toArray();
        
        $data = \DB::connection($this->selectdb())->select("SELECT sum(1) as tot, account_id as account,log_time,log_date FROM tb_user_logout WHERE log_time >= ? AND log_time <= ? GROUP BY account,log_date order by log_time desc", [$start, $end]);

        //$total = \DB::connection($this->selectdb())->table($tableAccount)->count();
        $total = 100;
        \Log::debug(__METHOD__, [$start, $end]);
        // $data = json_decode($data, true);
        $data = $this->groupData($data);

        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }


    public function selectdb($select = ''){
        if ($select) {
            return $this->_db_select = $select;
        }
        return $this->_db_select;
    }

    public function getConnectionName(){
        return $this->selectdb();
    }
    public function getTable($table = ''){
    
        if ($table) {

            $this->table = $table;
        }

        return $this->table;
    }


    // 统计计算数据
    public function groupData($re){
        $res = [];
        foreach($re as $obj) {
            $date = date('Y-m-d', strtotime($obj->log_time));
            $count = $obj->tot;

            if ($count > $this->_max_count) {
                $this->_max_count = $count;
            }
            if ($count > 5) {
                $res[$date]['fld_'.'6']++;
            } else {
                $res[$date]['fld_'.$count]++;
            }
            $res[$date]['log_time'] = $date; 
            $res[$date]['all']++; 
            $res[$date]['column'] += $count;
            
        }

        foreach ($res as &$item) {
            foreach($item as $attr => &$val){
                if (stripos($attr, 'fld_') !== false && $item['all']) {
                    $f =str_replace('fld_', '', $attr);
                    $attrRate = 'rate_'. $f;
                    $item[$attrRate] = round($val / $item['all'] * 100, 2) . '%';
                    $item['show_'. $f] = $val . "({$item[$attrRate]})";
                }
            }
        }
        return $res;
    }

    public static function getInput(){
        $log_time_arr = Request::get('log_time') ?: [];
        $start = $log_time_arr['start'];
        $end = $log_time_arr['end'];
        $start = is_numeric($start) && $start ?date("Y-m-d", $start) : $start;
        $end = is_numeric($end)&& $end ? date("Y-m-d",$end) : $end;
        !$start && $start = date('Y-m-d', strtotime('-7 day'));
        !$end && $end = date('Y-m-d');
        \Log::debug(__METHOD__, [date('Y-m-d H:i:s')]);
        $end .= ' 23:59:59';
        return [$start, $end];
    }
}
